package com.freak.neteasecloudmusic.modules.find.recommend.base;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.freak.guidepage.GuideBanner;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.base.BaseAbstractMvpFragment;
import com.freak.neteasecloudmusic.dialog.LoadingViewDialog;
import com.freak.neteasecloudmusic.modules.base.adapter.MainTabAdapter;
import com.freak.neteasecloudmusic.modules.find.recommend.base.adapter.NewSongBaseAdapter;
import com.freak.neteasecloudmusic.modules.find.recommend.base.entity.NewAlbumEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.base.entity.NewSongBaseEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.base.entity.RecommendEveryDayEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.base.entity.RecommendnewSongEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.base.fragment.RankingListFragment;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.BannerEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.SongListActivity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.adapter.SongListFragmentAdapter;
import com.freak.neteasecloudmusic.net.log.LogUtil;
import com.freak.neteasecloudmusic.utils.TimeUtils;
import com.freak.neteasecloudmusic.utils.ToastUtil;
import com.freak.neteasecloudmusic.view.custom.viewpager.CustomViewPager;
import com.github.rubensousa.gravitysnaphelper.GravitySnapHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2019/3/15.
 */

public class RecommendFragment extends BaseAbstractMvpFragment<RecommendPresenter> implements RecommendContract.View, GuideBanner.Adapter<ImageView, BannerEntity.BannersBean> {

    @BindView(R.id.guideBannerFind)
    GuideBanner guideBannerFind;
    @BindView(R.id.linearLayoutFindEveryDayRecommend)
    LinearLayout linearLayoutFindEveryDayRecommend;
    @BindView(R.id.linearLayoutFindSongList)
    LinearLayout linearLayoutFindSongList;
    @BindView(R.id.linearLayoutFindRand)
    LinearLayout linearLayoutFindRand;
    @BindView(R.id.linearLayoutFindRadioStation)
    LinearLayout linearLayoutFindRadioStation;
    @BindView(R.id.linearLayoutFindLive)
    LinearLayout linearLayoutFindLive;
    @BindView(R.id.linearLayoutFind)
    LinearLayout linearLayoutFind;
    @BindView(R.id.textViewFindRecommendSongList)
    TextView textViewFindRecommendSongList;
    @BindView(R.id.textViewFindRecommendSongListTip)
    TextView textViewFindRecommendSongListTip;
    @BindView(R.id.textViewFindRecommendSongListAll)
    TextView textViewFindRecommendSongListAll;
    @BindView(R.id.textViewFindRecommendStyle)
    TextView textViewFindRecommendStyle;
    @BindView(R.id.textViewFindRecommendStyleTip)
    TextView textViewFindRecommendStyleTip;
    @BindView(R.id.textViewFindRecommendStyleAll)
    TextView textViewFindRecommendStyleAll;
    @BindView(R.id.textViewFindRecommendScenes)
    TextView textViewFindRecommendScenes;
    @BindView(R.id.textViewFindRecommendScenesTip)
    TextView textViewFindRecommendScenesTip;
    @BindView(R.id.textViewFindRecommendScenesAll)
    TextView textViewFindRecommendScenesAll;
    @BindView(R.id.textViewFindRecommendYunCunFeatured)
    TextView textViewFindRecommendYunCunFeatured;
    @BindView(R.id.textViewFindRecommendYunCunFeaturedTip)
    TextView textViewFindRecommendYunCunFeaturedTip;
    @BindView(R.id.textViewFindRecommendYunCunFeaturedAll)
    TextView textViewFindRecommendYunCunFeaturedAll;
    @BindView(R.id.textViewFindRecommendLiveVoice)
    TextView textViewFindRecommendLiveVoice;
    @BindView(R.id.textViewFindRecommendLiveVoiceTip)
    TextView textViewFindRecommendLiveVoiceTip;
    @BindView(R.id.textViewFindRecommendLiveVoiceAll)
    TextView textViewFindRecommendLiveVoiceAll;
    @BindView(R.id.textViewFindRecommendEveryDayNewSong)
    TextView textViewFindRecommendEveryDayNewSong;
    @BindView(R.id.textViewFindRecommendEveryDayNewSongTip)
    TextView textViewFindRecommendEveryDayNewSongTip;
    @BindView(R.id.textViewFindRecommendEveryDayNewDiscTip)
    TextView textViewFindRecommendEveryDayNewDiscTip;
    @BindView(R.id.textViewFindRecommendEveryDayNewSongAll)
    TextView textViewFindRecommendEveryDayNewSongAll;
    @BindView(R.id.textViewFindRecommendEveryoneIsListening)
    TextView textViewFindRecommendEveryoneIsListening;
    @BindView(R.id.textViewFindRecommendEveryoneIsListeningTip)
    TextView textViewFindRecommendEveryoneIsListeningTip;
    @BindView(R.id.textViewFindRecommendEveryoneIsListeningAll)
    TextView textViewFindRecommendEveryoneIsListeningAll;
    @BindView(R.id.textViewFindRecommendLeaderBoard)
    TextView textViewFindRecommendLeaderBoard;
    @BindView(R.id.textViewFindRecommendLeaderBoardTip)
    TextView textViewFindRecommendLeaderBoardTip;
    @BindView(R.id.textViewFindRecommendLeaderBoardAll)
    TextView textViewFindRecommendLeaderBoardAll;
    @BindView(R.id.textViewFindRecommendRadio)
    TextView textViewFindRecommendRadio;
    @BindView(R.id.textViewFindRecommendRadioTip)
    TextView textViewFindRecommendRadioTip;
    @BindView(R.id.textViewFindRecommendRadioAll)
    TextView textViewFindRecommendRadioAll;
    @BindView(R.id.viewFindRecommendEveryDayNewSongTip)
    View viewFindRecommendEveryDayNewSongTip;
    @BindView(R.id.recyclerViewEveryDayNewSong)
    RecyclerView recyclerViewEveryDayNewSong;
    @BindView(R.id.recyclerViewEveryDayNewDisc)
    RecyclerView recyclerViewEveryDayNewDisc;
    @BindView(R.id.constraintLayoutNewSongOrNewDisc)
    ConstraintLayout constraintLayoutNewSongOrNewDisc;
    @BindView(R.id.recyclerViewFindRecommend)
    RecyclerView recyclerViewFindRecommend;
    @BindView(R.id.recyclerViewStyle)
    RecyclerView recyclerViewStyle;
    @BindView(R.id.recyclerViewScenes)
    RecyclerView recyclerViewScenes;
    @BindView(R.id.recyclerViewYunCunFeatured)
    RecyclerView recyclerViewYunCunFeatured;
    @BindView(R.id.recyclerViewLiveVoice)
    RecyclerView recyclerViewLiveVoice;
    @BindView(R.id.recyclerViewFindEveryoneIsListening)
    RecyclerView recyclerViewFindEveryoneIsListening;
    @BindView(R.id.recyclerViewFindRecommendRadio)
    RecyclerView recyclerViewFindRecommendRadio;
    @BindView(R.id.customViewPagerFindRecommendLeaderBoard)
    CustomViewPager customViewPagerFindRecommendLeaderBoard;
    private SongListFragmentAdapter mSongListAdapter;
    private List<MultiItemEntity> list;
    private List<NewSongBaseEntity> newSongList;
    private List<NewSongBaseEntity> newALbumList;
    private NewSongBaseAdapter newSongBaseAdapter;
    private NewSongBaseAdapter newAlbumAdapter;
    private MainTabAdapter rankIngListAdater;
    private List<String> titleList;
    private List<Fragment> fragmentList;


    @Override
    public void showToast(String toast) {
        LoadingViewDialog.getInstance().dismiss();
        ToastUtil.shortShow(toast);
    }

    @Override
    protected RecommendPresenter createPresenter() {
        return new RecommendPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void initEventAndData() {
        loadBanner();
        mPresenter.getRecommendEveryDay();
//        mPresenter.getNewEst();
        mPresenter.getRecommendNewSong();
        mPresenter.getTopNewEst(0, 9);
    }

    @Override
    protected void initView(View view) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mActivity);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        list = new ArrayList<>();
        recyclerViewFindRecommend.setLayoutManager(linearLayoutManager);
        mSongListAdapter = new SongListFragmentAdapter(list, mActivity);
        mSongListAdapter.bindToRecyclerView(recyclerViewFindRecommend);
        recyclerViewFindRecommend.setAdapter(mSongListAdapter);
        recyclerViewFindRecommend.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildLayoutPosition(view) != list.size() - 1) {
                    outRect.right = 20;
                }
            }
        });

        textViewFindRecommendEveryDayNewSong.setText(TimeUtils.format(TimeUtils.DEFAULT_PATTERN_1, System.currentTimeMillis()));
        newSongList = new ArrayList<>();
        newSongBaseAdapter = new NewSongBaseAdapter(R.layout.view_item_new_song_base, newSongList, mActivity);
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(mActivity);
        linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewEveryDayNewSong.setLayoutManager(linearLayoutManager1);
        GravitySnapHelper gravitySnapHelper = new GravitySnapHelper(Gravity.START);
        gravitySnapHelper.attachToRecyclerView(recyclerViewEveryDayNewSong);
        newSongBaseAdapter.bindToRecyclerView(recyclerViewEveryDayNewSong);
        recyclerViewEveryDayNewSong.setAdapter(newSongBaseAdapter);
        recyclerViewEveryDayNewSong.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildAdapterPosition(view) == newSongList.size() - 1) {
                    outRect.right = 40;
                }
            }
        });

        newALbumList = new ArrayList<>();
        newAlbumAdapter = new NewSongBaseAdapter(R.layout.view_item_new_song_base, newALbumList, mActivity);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(mActivity);
        linearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerViewEveryDayNewDisc.setLayoutManager(linearLayoutManager2);
        GravitySnapHelper gravitySnapHelper1 = new GravitySnapHelper(Gravity.START);
        gravitySnapHelper1.attachToRecyclerView(recyclerViewEveryDayNewDisc);
        newAlbumAdapter.bindToRecyclerView(recyclerViewEveryDayNewDisc);
        recyclerViewEveryDayNewDisc.setAdapter(newAlbumAdapter);
        recyclerViewEveryDayNewDisc.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildAdapterPosition(view) == newALbumList.size() - 1) {
                    outRect.right = 40;
                }
            }
        });

        setSelectNewSong(true);

        titleList = new ArrayList<>();
        titleList.add("云音乐新歌榜");
        titleList.add("云音乐热歌榜");
        titleList.add("网易原创歌曲榜");
        titleList.add("云音乐飙升榜");
        titleList.add("云音乐电音榜");
        fragmentList = new ArrayList<>();
        for (int i = 1; i < 5; i++) {
            Bundle bundle = new Bundle();
            bundle.putString("idx", String.valueOf(i));
            RankingListFragment rankIngListFragment = new RankingListFragment();
            rankIngListFragment.setArguments(bundle);
            fragmentList.add(rankIngListFragment);
        }
        customViewPagerFindRecommendLeaderBoard.setScanScroll(true);
        rankIngListAdater = new MainTabAdapter(mActivity.getSupportFragmentManager(), fragmentList, titleList);
        customViewPagerFindRecommendLeaderBoard.setAdapter(rankIngListAdater);
        customViewPagerFindRecommendLeaderBoard.setPageMargin(7);
        customViewPagerFindRecommendLeaderBoard.setCurrentItem(0);
    }

    private void setSelectNewSong(boolean isNewSong) {
        if (isNewSong) {
            textViewFindRecommendEveryDayNewSongTip.setSelected(true);
            textViewFindRecommendEveryDayNewDiscTip.setSelected(false);
            recyclerViewEveryDayNewSong.setVisibility(View.VISIBLE);
            recyclerViewEveryDayNewDisc.setVisibility(View.GONE);
        } else {
            textViewFindRecommendEveryDayNewSongTip.setSelected(false);
            textViewFindRecommendEveryDayNewDiscTip.setSelected(true);
            recyclerViewEveryDayNewSong.setVisibility(View.GONE);
            recyclerViewEveryDayNewDisc.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void initLazyData() {

    }

    public void loadBanner() {
        mPresenter.loadBanner();
        LoadingViewDialog.getInstance().show(mActivity);
    }

    @Override
    protected void showLoading() {

    }

    @Override
    public void loadBannerSuccess(BannerEntity bannerEntity) {
        LoadingViewDialog.getInstance().dismiss();
        LogUtil.e("bannerEntity " + bannerEntity.toString());
        if (bannerEntity.getBanners() != null && bannerEntity.getBanners().size() > 0) {
            /**
             * 设置是否开启自动轮播，需要在 setData 方法之前调用，并且调了该方法后必须再调用一次 setData 方法
             * 例如根据图片当图片数量大于 1 时开启自动轮播，等于 1 时不开启自动轮播
             */
            guideBannerFind.setAutoPlayAble(bannerEntity.getBanners().size() > 1);

            guideBannerFind.setAdapter(this);

            guideBannerFind.setData(bannerEntity.getBanners(), null);
        }

    }

    @Override
    public void getRecommendEveryDaySuccess(RecommendEveryDayEntity model) {
        list.clear();
        if (model != null) {
            if (model.getRecommend() != null && model.getRecommend().size() > 0) {
                list.addAll(model.getRecommend());
            }
        }
        mSongListAdapter.notifyDataSetChanged();
    }

    @Override
    public void getRecommendNewSongSuccess(RecommendnewSongEntity model) {
        newSongList.clear();
        if (model != null) {
            List<MultiItemEntity> resultBeans = new ArrayList<>();
            for (int i = 0; i < model.getResult().size(); i++) {
                resultBeans.add(model.getResult().get(i));
                if (resultBeans.size() >= 3) {
                    List<MultiItemEntity> list = new ArrayList<>(resultBeans);
                    newSongList.add(new NewSongBaseEntity(list));
                    resultBeans.clear();
                }
            }
        }
        newSongBaseAdapter.notifyDataSetChanged();
    }

    /**
     * 最新专辑
     *
     * @param model
     */
    @Override
    public void getNewEstSuccess(NewAlbumEntity model) {

    }

    /**
     * 新碟上架
     *
     * @param model
     */
    @Override
    public void getTopNewEstSuccess(NewAlbumEntity model) {
        newALbumList.clear();
        if (model != null) {
            List<MultiItemEntity> resultBeans = new ArrayList<>();
            for (int i = 0; i < model.getAlbums().size(); i++) {
                resultBeans.add(model.getAlbums().get(i));
                if (resultBeans.size() >= 3) {
                    List<MultiItemEntity> list = new ArrayList<>(resultBeans);
                    newALbumList.add(new NewSongBaseEntity(list));
                    resultBeans.clear();
                }
            }
        }
        newAlbumAdapter.notifyDataSetChanged();
    }


    @OnClick({R.id.textViewFindRecommendEveryDayNewDiscTip, R.id.textViewFindRecommendEveryDayNewSongTip, R.id.linearLayoutFindEveryDayRecommend, R.id.linearLayoutFindSongList, R.id.linearLayoutFindRand, R.id.linearLayoutFindRadioStation, R.id.linearLayoutFindLive})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linearLayoutFindEveryDayRecommend:

                break;
            case R.id.linearLayoutFindSongList:
                gotoActivity(SongListActivity.class);
                break;
            case R.id.linearLayoutFindRand:

                break;
            case R.id.linearLayoutFindRadioStation:

                break;
            case R.id.linearLayoutFindLive:
                break;
            case R.id.textViewFindRecommendEveryDayNewSongTip:
                setSelectNewSong(true);
                break;
            case R.id.textViewFindRecommendEveryDayNewDiscTip:
                setSelectNewSong(false);
                break;
        }
    }

    @Override
    public void fillBannerItem(GuideBanner banner, ImageView itemView, @Nullable BannerEntity.BannersBean model, int position) {
        Glide.with(itemView.getContext())
                .load(model.getImageUrl())
                .apply(new RequestOptions().placeholder(R.mipmap.holder).error(R.mipmap.holder).dontAnimate().centerCrop())
                .into(itemView);
    }
}
